from fastapi import APIRouter, Query,UploadFile
from tortoise.expressions import Q
from app.controllers.marketing.integral_rule import integral_rule_controller
from app.models import CustomerUser
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.goods import *
from app.schemas.marketing import IntegralRoleUpdate, IntegralRoleCreate

integral_rule_router = APIRouter()
model_name = "积分规则"


@integral_rule_router.get("/list", summary=f"查看{model_name}列表")
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        name: str = Query(None, description=f"名称"),
):
    q = Q()
    if name:
        q &= Q(name__contains=name)
    total, data = await integral_rule_controller.do_list(page=page, page_size=page_size, search=q)

    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@integral_rule_router.get("/get", summary=f"查看{model_name}")
async def do_get(
        data_id: int = Query(..., description="ID"),
):
    data = await integral_rule_controller.do_detail(data_id=data_id)
    return Success(data=data)

@integral_rule_router.post("/create", summary=f"创建{model_name}")
async def do_create(
        category_in: IntegralRoleCreate,
):
    await integral_rule_controller.do_create(obj_in=category_in)
    return Success(msg="创建成功")

@integral_rule_router.post("/update", summary=f"更新{model_name}")
async def do_update(
        category_in: IntegralRoleUpdate,
):
    await integral_rule_controller.do_update(obj_in=category_in)
    return Success(msg="更新成功")

@integral_rule_router.delete("/delete", summary=f"删除{model_name}")
async def do_delete(
        data_id: int = Query(..., description="ID"),
):
    await integral_rule_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")

@integral_rule_router.get("/rules", summary="获取规则下拉")
async def list_rules():
    level_objs = await integral_rule_controller.level_list()
    data = []
    for level in level_objs:
        if level:
            data.append({"label": level_objs[level], "value": level})
    return Success(data=data)
